package com.examplemartin.jpademo.service;

import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.dao.CustDao;
import com.examplemartin.jpademo.pojo.po.cust.CustPO;
import com.examplemartin.jpademo.util.EntityCheckUtil;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
@Component
public class CustService {

    @Resource
    private CustDao custDao;
    public static Integer CUST_STATUS_ON = 1;
    public static Integer CUST_STATUS_LOCK = -1;

    /**
     * 查询
     * @return
     */
    // 01 返回所有客户
    public List<CustPO> findAll(){
        List<CustPO> all = custDao.findAll();
        return all;
    }
    // 02 按id查找
    public CustPO findById(Long id){
        return custDao.findById(id).get();
    }

    // 03 按状态查找
    public List<CustPO> findAllByStatus(Integer status){
        return custDao.findAllByStatus(status);
    }


    /**
     * 新增顾客 ajax
     * /addcust.ajax
     *
     * @param custPO
     *
     * @return
     */
    public AjaxResult addcustAjax(@RequestBody CustPO custPO) {

        AjaxResult check = EntityCheckUtil.check(custPO);
        if(check.getCode() != 200)
        {
            return check;
        }

        // 按顾客名和地址
        String name = custPO.getName().trim();
        String address = custPO.getName().trim();
        List<CustPO> allByNameAndAddress = custDao.findAllByNameAndAddress(name, address);
        if(allByNameAndAddress!=null && allByNameAndAddress.size()!=0){
            return  AjaxResult.fail(500,name + ": 顾客已存在");
        }

        CustPO save = custDao.save(custPO);
        if (save!=null){
            return AjaxResult.success(200,"添加用户: "+custPO.getName(),null);
        }else {
            return AjaxResult.fail(500,"添加用户失败: "+custPO.getName());
        }
    }

    /**
     * 禁用顾客
     * @param custPO
     * @return
     */
    public AjaxResult forbiddenAjax(@RequestBody CustPO custPO){

        Optional<CustPO> byId = custDao.findById(custPO.getId());
        if(byId == null){
            return AjaxResult.fail(500,"顾客数据有误,请检查");
        }

        CustPO custPObyId = byId.get();
        custPObyId.setStatus(CustService.CUST_STATUS_LOCK);
        CustPO save = custDao.save(custPObyId);
        if(save == null){
            return AjaxResult.fail(500,"顾客数据更新有误,请检查");
        }else{
            return AjaxResult.success(200,save.getName()+": 被锁定",null);
        }

    }

    /**
     * 修改 顾客信息
     * /editcust.ajax
     * @param custPO
     * @return
     */
    public AjaxResult editcustAjax(CustPO custPO){

        AjaxResult check = EntityCheckUtil.check(custPO);
        if(check.getCode()!=200){
            return check;
        }

        Optional<CustPO> byId = custDao.findById(custPO.getId());
        if(byId == null ){
            return AjaxResult.fail(500,"客户数据有错误,请检查");
        }

        CustPO editCust = byId.get();
        editCust.setStatus(custPO.getStatus());
        editCust.setName(custPO.getName());
        editCust.setAddress(custPO.getAddress());
        editCust.setLinkman(custPO.getLinkman());
        editCust.setComment(custPO.getComment());
        editCust.setEmail(custPO.getEmail());
        editCust.setPhone(custPO.getPhone());
        CustPO save = custDao.save(editCust);
        if(save==null){
            return AjaxResult.fail(500,"修改失败,请检查");
        }else
        {
            return AjaxResult.success(200,"客户: "+editCust.getName()+" 信息已更新",null);
        }

    }




}
